/* Bagian 1. ADT System Modus 1 : Load, Save, AddSite, CreateTable */

/* File			: MSystemS.c									*/
/* Deskripsi	: realisasi (body) dari MSystemS.h				*/
/* Isi			: realisasi / kode program dari semua prototype	*/
/*				  yg didefinisikan pada MSystemS.h				*/

/* ADT system */

#include "system.h"
#include "..\CGRAPHICS\cgraphics.h"

void ParseSaveCommand(Reader R) {
	bool SAVE_OK;
	if(R.nArgument == 1) {
		if(IsStringStrictEqual(SOURCEFILE,UNDEFINEDFILE)) {
			StartPrintMsg(); printf("ERROR : Lokasi penyimpanan belum didefinisi");
		}
		else {
			Save(SOURCEFILE, &SAVE_OK);
			if(SAVE_OK) { StartPrintMsg(); SetColor(FGI); printf("Save ke %s berhasil",SOURCEFILE.s); }
		}
	}
	else {
		Save(R.argument[1], &SAVE_OK);
		if(SAVE_OK) { StartPrintMsg(); SetColor(FGI); printf("Save ke %s berhasil",R.argument[1].s); }
	}
}

int WriteStringToFile(FILE *f, string s) {
	if(IsStringHasWhiteSpace(s))
		return fprintf(f, "\"%s\" ", s.s);
	else
		return fprintf(f, "%s ", s.s);
}

/* BODY prototype */
	void Save (string FileName, bool *OK)
		/* Modus Persiapan : Save */
		/* Mengeksekusi semua hal yang dilakukan dalam command Save sesuai dengan spek soal */
		{
			/* KAMUS LOKAL */
			/* Semua nama yang dipakai dalam BADAN PROSEDUR */
				char fname[50];
				sprintf(fname,"%s.in",FileName.s);
				FILE *f; int retval;
				int NumbNodes = 0, NumbEdges = 0, i, j;
				nodeAddress Q = FIRST(NODELIST); AddressMhs QMhs = FIRST(TABELMHS); AddressMK QMK = FIRST(TABELMK);
				AddressPMK QPMK = FIRST(TABELPMK); ownerAddress QOw; edgeAddress Qiu = FIRST(EDGELIST);
				string Tanggal;
		
			/* ALGORITMA */
			/* Deretan intruksi pemberian harga, input, output,
			   analisis kasus, pengulangan, atau prosedur */
				f = fopen(fname, "w");
				if (f != NULL)
					{
						*OK = true;
						retval = fprintf(f, "@nodes\n");
						while (Q != NULL)
							{NumbNodes++; Q = NEXT(Q);}
						retval = fprintf(f, "%d #\n", NumbNodes);
						Q = FIRST(NODELIST);
						for (i = 1; i <= NumbNodes; i++)
							{
								string nodename = NODENAME(Q);
								AddStrF(nodename.s,"@@");
								retval = WriteStringToFile(f,nodename);
								retval = fprintf(f, "\n");
								if (HAS_SERVER(Q))
									{
										if (HAS_CLIENT(Q))
											{retval = fprintf(f, "Y Y %.4f %.4f %.4f %.4f #\n", RETRIEVETIME(Q), UPDATETIME(Q), INSERTTIME(Q), DELETETIME(Q));}
										else
											{retval = fprintf(f, "Y T %.4f %.4f %.4f %.4f #\n", RETRIEVETIME(Q), UPDATETIME(Q), INSERTTIME(Q), DELETETIME(Q));}
									}
								else
									{
										if (HAS_CLIENT(Q))
											{retval = fprintf(f, "T Y %.4f %.4f %.4f %.4f #\n", RETRIEVETIME(Q), UPDATETIME(Q), INSERTTIME(Q), DELETETIME(Q));}
										else
											{retval = fprintf(f, "T T %.4f %.4f %.4f %.4f #\n", RETRIEVETIME(Q), UPDATETIME(Q), INSERTTIME(Q), DELETETIME(Q));}
									}
								if (HAS_TMHS(Q))
									{
										retval = WriteStringToFile(f,NAMATABELMHS);
										retval = fprintf(f, " #\n");
										QMhs = FIRST(TABELMHS);
										while (QMhs != NULL)
											{
												QOw = FIRST(QMhs->_ownerList);
												if(SearchOwnerByNode(&OWNERLIST(QMhs),Q)!=NULL)
															{
																retval = WriteStringToFile(f,NIM(QMhs));
																retval = WriteStringToFile(f,NAMA(QMhs));
																retval = WriteStringToFile(f,DateToString(TGLLAHIR(QMhs)));
																Tanggal = DateToString(TGLLAHIR(QMhs));
																/*for (j = 0; j < LongStr(StringToChar(Tanggal)); j++)
																	{
																		if (Tanggal.s[j] == '/')
																			{retval = fprintf(f, "%c", '-');}
																		else
																			{retval = fprintf(f, "%c", Tanggal.s[j]);}
																	}*/
																retval = WriteStringToFile(f,KOTAASAL(QMhs));
																retval = WriteStringToFile(f,(IPK(QMhs)==TABELMHS_UNDEF_VAL) ? CharToString("NULL") : FloatToString(IPK(QMhs)));
																retval = WriteStringToFile(f,(JMLSKS(QMhs)==TABELMHS_UNDEF_VAL) ? CharToString("NULL") : IntToString(JMLSKS(QMhs)));
																retval = fprintf(f, "#\n");
															}
												QMhs = QMhs->next;
											}
									}
								if (HAS_TMK(Q))
									{
										retval = WriteStringToFile(f,NAMATABELMK);
										retval = fprintf(f, " #\n");
										QMK = FIRST(TABELMK);
										while (QMK != NULL)
											{
												if (SearchOwnerByNode(&OWNERLIST(QMK),Q)!=NULL)
															{
																retval = WriteStringToFile(f,KODEMK(QMK));
																retval = WriteStringToFile(f,NAMA_MK(QMK));
																retval = WriteStringToFile(f,IntToString(SKS(QMK)));
																retval = WriteStringToFile(f,BIDANG(QMK));
																retval = fprintf(f, "#\n");
															}
												QMK = QMK->next;
											}
									}
								if (HAS_TPMK(Q))
									{
										retval = WriteStringToFile(f,NAMATABELPMK);
										retval = fprintf(f, " #\n");
										QPMK = FIRST(TABELPMK);
										while (QPMK != NULL)
											{
												if (SearchOwnerByNode(&OWNERLIST(QPMK),Q)!=NULL)
															{
																retval = WriteStringToFile(f,NIM(MHS(QPMK)));
																retval = WriteStringToFile(f,KODEMK(MK(QPMK)));
																retval = WriteStringToFile(f,IntToString(SEMESTER(QPMK)));
																retval = WriteStringToFile(f,ThnAkademikToString(TAHUN(QPMK)));
																retval = fprintf(f, "%c #\n", NILAI(QPMK));
															}
												QPMK = QPMK->next;
											}
									}
								Q = NEXT(Q);
							}
						retval = fprintf(f, "@edges\n");
						while (Qiu != NULL)
							{NumbEdges++; Qiu = NEXT(Qiu);}
						retval = fprintf(f, "%d #\n", NumbEdges);
						Qiu = FIRST(EDGELIST);
						for (i = 1; i <= NumbEdges; i++)
							{
								retval = WriteStringToFile(f,NODENAME(GetA(Qiu)));
								retval = WriteStringToFile(f,NODENAME(GetB(Qiu)));
								retval = WriteStringToFile(f,FloatToString(GetUpload(Qiu)));
								retval = WriteStringToFile(f,FloatToString(GetDownload(Qiu)));
								retval = fprintf(f,"#\n");
								Qiu = NEXT(Qiu);
							}
						retval = fprintf(f, "@end");
					}
				else
					{*OK = false;}
				fclose(f);
		}
